function formatTime(date1,str='yy-mm-dd h:m:s') {
	let date = new Date(date1);
	let d = {
		"yy" : date.getFullYear(),
		"mm" : date.getMonth() + 1,
		"dd" : date.getDate(),
		"h" : date.getHours(),
		"m" : date.getMinutes(),
		"s" : date.getSeconds()
	}
	
	let arr1 = str.split(" ")
	let arrY = []
	let arrH = []
	let strY = ''
	if(arr1[0] && arr1[0].indexOf("-") != -1){
		strY = '-'
		arr1[0].split('-').forEach((e,i)=>{
			arrY[i] = d[e]
		})
	}else if(arr1[0].indexOf("/") != -1){
		strY = '/'
		arr1[0].split('/').forEach((e,i)=>{
			arrY[i] = d[e]
		})
	}
	if(arr1[1] && arr1[1].indexOf(":") != -1){
		arr1[1].split(':').forEach((e,i)=>{
			arrH[i] = d[e]
		})
	}
	return arrY.map(formatNumber).join(strY) + ' ' + arrH.map(formatNumber).join(':')
}

function formatNumber(n) {
	n = n.toString()
	return n[1] ? n : '0' + n
}
function checkTime(i) {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}

function dateAdd(d,num){
    var d = new Date(d.substring(0,4),
        d.substring(5,7)-1,
        d.substring(8,10),
        d.substring(11,13),
        d.substring(14,16),
        d.substring(17,19));
    d.setTime( d.getTime() + num*1000);
    return d.getFullYear() +"-" 
         + checkTime((d.getMonth() + 1))
         + "-" + checkTime(d.getDate())
         + " " + checkTime(d.getHours())
         + ":" + checkTime(d.getMinutes())
         + ":" + checkTime(d.getSeconds());
}
function checkAuditTime(startTime, s){
    const date  = new Date()
    let Time = new Date(dateAdd(startTime,s))
    let endTime = Time.getTime();
    let newDate = date.getTime()
    return endTime>newDate? false:true
}

// 判断日期是不是今天、昨天、明天
const isToday = (str) => {
  let d = new Date(str).setHours(0, 0, 0, 0);
  let today = new Date().setHours(0, 0, 0, 0);

  let obj = {
    '-86400000': '昨天',
    0: '今天',
    86400000: '明天',
  };

  return obj[d - today] || '啥也不是';
};

// 'yyyy/MM/dd' => 2022/09/03
// 'yyyy/MM/dd hh:mm:ss' => 2022/09/03 16:11:41
// 'mm:ss' => 18:28
const format = (date,fmt)=>{
  var o = { 
      "M+" : date.getMonth()+1,                 //月份 
      "d+" : date.getDate(),                    //日 
      "h+" : date.getHours(),                   //小时 
      "m+" : date.getMinutes(),                 //分 
      "s+" : date.getSeconds(),                 //秒 
      "q+" : Math.floor((date.getMonth()+3)/3), //季度 
      "S"  : date.getMilliseconds()             //毫秒 
  }; 
  //(y+)匹配多个y，比如yyyy
  if(/(y+)/.test(fmt)) {
       // RegExp.$1是RegExp的一个属性,指的是与正则表达式匹配的第一个 子匹配(以括号为标志)字符串
          fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length)); 
  }
   for(var k in o) {
      if(new RegExp("("+ k +")").test(fmt)){
           fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
       }
   }
  return fmt; 
 }

 //返回消息列表时间
 function setTime(str){
  let d = new Date(str).setHours(0, 0, 0, 0);
  let today = new Date().setHours(0, 0, 0, 0);

  let obj = {
    '-86400000': '昨天',
    0: '今天',
    86400000: '明天',
  };
  let day ='';
  if(obj[d - today]==='今天'){
    day = new Date(str).getHours()+':'+new Date(str).getMinutes()
  }else if(obj[d - today]==='昨天'){
    day = obj[d - today];
  }else{
    day = new Date(str).getMonth()+'-'+new Date(str).getDate()
  }
  return day;
 }

export default {
	formatTime,
  checkAuditTime,
  isToday,
  format,
  setTime
}